package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ExamineClassEntity
import com.open.party.cloud.model.ExamineClassJoinVO
import com.open.party.cloud.model.ExamineSignBean
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.dialog.bottomDialog.normalDialog.BottomSelectorDialog
import com.sinothk.dialog.bottomDialog.normalDialog.BottomSelectorDialog.SelectorDialogListener
import kotlinx.android.synthetic.main.examine_join_class_activity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*

class ExamineJoinClassActivity : AppEventBusTitleBaseActivity() {
    var viewModel: ExamineViewModel? = null
    private var classId: String? = null
    var vo: ExamineClassJoinVO? = null

    override fun getLayoutResId(): Int = R.layout.examine_join_class_activity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setViewTitle("加入班级")
        classId = intent.getStringExtra("classId") as String
        initView()

        viewModel = ExamineViewModel()
        viewModel!!.getSchoolClassesInfo(classId!!)

        submitBtn.setOnClickListener {
            submit()
        }
    }

    private fun initView() {
        vo = ExamineClassJoinVO()
        vo!!.classId = classId
        val user = LocalCache.getLoginUser()
        vo!!.userId = user.id

        userNameEt.setText(XUtils.string().getNotNullValue(user.name))
        phoneNumEt.setText(XUtils.string().getNotNullValue(user.mainmobile))
//        unitEt.setText(XUtils.string().getNotNullValue(user.orgName))
//        sexEt.setText(XUtils.string().getNotNullValue(user.sex.desc))

        sexItem.setOnClickListener {
            selectSex()
        }
    }

    private fun submit() {
        val userName = userNameEt.text.toString()
        if (XUtils.string().isEmpty(userName)) {
            XUtils.toast().show("请输入姓名")
            return
        }
        vo!!.userName = userName

        val phoneNum = phoneNumEt.text.toString()
        if (XUtils.string().isEmpty(phoneNum)) {
            XUtils.toast().show("请输入电话号码")
            return
        }
        if (phoneNum.length != 11 || '1' != phoneNum[0]) {
            XUtils.toast().show("电话号码可能不正确")
            return
        }
        vo!!.userPhone = phoneNum

        val unitName = unitEt.text.toString()
        if (XUtils.string().isEmpty(unitName)) {
            XUtils.toast().show("请输入单位")
            return
        }
        vo!!.unitName = unitName

        if (XUtils.string().isEmpty(vo!!.sex)) {
            XUtils.toast().show("请选择性别")
            return
        }

//        val zzmm = zzmmEt.text.toString()
//        if (XUtils.string().isEmpty(zzmm)) {
//            XUtils.toast().show("请输入政治面貌")
//            return
//        }
//        vo!!.zzmm = zzmm
//
//        val rdsh = rdshEt.text.toString()
//        if (XUtils.string().isEmpty(rdsh)) {
//            XUtils.toast().show("请输入入党时间")
//            return
//        }
//        vo!!.rdsh = rdsh

        showLoadingDialog("正在提交")
//        viewModel!!.examineClassJoin(vo!!)
        viewModel!!.updateJoinClass(vo!!)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ExamineSignBean>) {
        if (result.eventType != "updateJoinClass") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("加入成功")
                finish()
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun getSchoolClassesInfoCallback(result: ResultInfo<ExamineClassEntity>) {
        if (result.eventType != "getSchoolClassesInfo") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setViewTitle("加入" + XUtils.string().getNotNullValue(result.data.className, "加入班级"))
                } else {
                    setViewTitle("加入班级")
                }

            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun selectSex() {
        val data = ArrayList<String>()
        data.add("男")
        data.add("女")
        BottomSelectorDialog.createBuilder(this, supportFragmentManager)
            .setTitle("选择性别")
            .setCancelButtonTitle("取消") //
            .setOtherButtonTitles(data)
            .setCancelableOnTouchOutside(true)
            .setListener(object : SelectorDialogListener {
                override fun onDismiss(selectorDialog: BottomSelectorDialog, b: Boolean) {}
                override fun onOtherButtonClick(selectorDialog: BottomSelectorDialog, index: Int) {
                    vo!!.sex = if (index == 0) {
                        "M"
                    } else {
                        "W"
                    }

                    sexTv.text = data[index]
                }
            }).show()
    }
}